<?php
namespace app\index\controller;

use AlibabaCloud\Dbs\Dbs;
use app\common\model\SystemCompany;
use app\common\model\SystemContract;
use app\common\model\SystemFrequencyDetailed;
use app\common\model\SystemMemberCount;
use think\Db;

class Service extends Base{
    public function index()
    {

        $type = $this->request->get();
        $company = SystemCompany::find($this->company['id']);
        $contract = SystemContract::where('mid','=',$this->company['mid'])->find();
        $list = Db::name('system_member_count as smc')
            ->join('system_service ss','smc.service_id = ss.id')
            ->where([
            'smc.member_id'=>$this->company['mid'],
            'smc.company_id'=>$this->company['id'],
            'smc.contract_id'=>$contract['id'],
            ])->field([
                'smc.member_id'=>'mid',
                'smc.surplus'=>'surplus',
                'smc.used'=>'used',
                'ss.title'=>'title',
                'smc.back'=>'back',
                'smc.service_id'=>'service_id',
            ])->all();
        if (isset($type['type']) && $type['type'] == 1){
            $this->list = $list;
            $this->fetch();
        }else{
            return $list;
        }
    }

    public function detail()
    {
        $parameter = $this->request->get();
        $this->data = Db::name('system_frequency_detailed as sfd')
            ->join('system_service ss','ss.id=sfd.service_id')
            ->where([
                'sfd.mid'=>$this->company['mid'],
                'sfd.type'=>$parameter['type'],
                'sfd.service_id'=>$parameter['service_id'],
                'sfd.specific'=>0,
            ])
            ->field([
                'sfd.create_at'=>'create_at',
                'sfd.desc'=>'desc',
                'ss.title'=>'title',
                'sfd.code'=>'code',
            ])->select();
        foreach ($this->data as &$item){
            if (strstr($item['desc'],'-')){
                $item['remark'] = substr($item['desc'],0,strrpos($item['desc'],'-'));
            }else{
                $item['remark'] = $item['desc'];
            }
        }
        $this->fetch('detail');
    }
}
